<?php
/**
* Letterman Newsletter Component
* 
* @package Letterman
* @author Soeren
* @copyright Soeren Eberhardt <soeren@virtuemart.net>
*   (who just needed an easy and *working* Newsletter component for Mambo 4.5.1 and mixed up Newsletter and YaNC)
* @copyright Mark Lindeman <mark@pictura-dp.nl> 
*    (parts of the Newsletter component by Mark Lindeman; Pictura Database Publishing bv, Heiloo the Netherland)
* @copyright Adam van Dongen <adam@tim-online.nl>
*    (parts of the YaNC component by Adam van Dongen, www.tim-online.nl)
*    
* @copyright Philip Sylvain <contact@sphilip.com>
*  	(MVC refactorization and AJAX)
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*
*/


// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.view');

class LettermanViewNewsletters extends JView

{
	var $pagination = null ;
	var $user = null;
	
	function display( $tpl = null )
	{
		$this->user = & JFactory::getUser();
		
		switch ($tpl) {
			case 'form':
				$this->editItem();
				break;
			case 'send':
				break;
			default :
				$this->listIems();
				
		}
		
		parent::display($tpl);
	}
	
	
	function listIems()
	{
		
		$model = $this->get('items');
		jimport( 'joomla.html.pagination' );
		$this->pagination = new JPagination($model['total'], $model['limitstart'], $model['limit']);
		$this->assignRef('rows' , $model['rows'] );
		$this->assignRef('search' , $model['search'] );
		
		JHTML::_('behavior.tooltip');
		JHTML::_('behavior.modal');
		
		JToolBarHelper::title( JText::_( 'Newsletter Manager' ), 'generic.png' );
		JToolBarHelper::publish();
    	JToolBarHelper::unpublish();
    	JToolBarHelper::addNew();
    	JToolBarHelper::editList();
    	JToolBarHelper::deleteList( JText::_( 'CONFIRM_DELETE_MSG' ) );
		
	}
	
	function editItem()
	{
		
		JRequest::setVar( 'hidemainmenu', 1 );
		
		$row =& $this->get('row');
		
		$file = JPATH_COMPONENT.DS.'views'.DS.'newsletters'.DS.'tmpl'.DS.'form.xml' ;
		$this->form = new JParameter('', $file );
		$this->form->set('published', $row->published );
		$this->form->set('access', $row->access );
		$this->form->set('publish_up', $row->publish_up );
		$this->form->set('publish_down', $row->publish_down );
		
		$title = $row->id ? JText::_( 'Edit newsletter' ) : JText::_( 'Add newsletter' );
		
		JToolBarHelper::title( $title , 'generic.png' );
		
		//dump($row);
		$this->assignRef('row' , $row );
		
		JToolBarHelper::save() ;
	    JToolBarHelper::apply() ;
	    JToolBarHelper::cancel() ;
		
	}
	
	function is_checkout( $checked_out )
	{
		if ($this->user && JTable::isCheckedOut( $this->user->get('id'), $checked_out ) ) {
			return true;
		}
		return false;
	}
	
	function renderLastSend( $date )
	{
		if ($date == '0000-00-00 00:00:00') {
			return JText::_( 'Never' );
		}
		
		return JHTML::_('date', $date , JText::_('DATE_FORMAT_LC2') );
	}
	
	function renderGridMsg( $text='', $html='')
	{
		$message = empty($text) ? $html : $text ;
		
		$message = strip_tags($message);
		
		if (strlen($message) > 80) {
			$message = substr($message, 0 , 78) . " ...";
		}
		
		return $message;
	}
	
	function getViewUrl( $params='', $xhtml=false )
	{
		$params = $params ? '&' .$params : '' ;
		return JRoute::_('index.php?option=com_letterman&controller=newsletters' . $params, $xhtml);

	}
	
	
}
